namespace GUI.Migrations
{
    using System;
    using System.Data.Entity;
    using System.Data.Entity.Migrations;
    using System.Linq;
    using POCOS;
using WebMatrix.WebData;
using System.Web.Security;

    internal sealed class Configuration : DbMigrationsConfiguration<DAL.AuteurContext>
    {
        public Configuration()
        {
            AutomaticMigrationsEnabled = false;
        }

        protected override void Seed(DAL.AuteurContext context)
        {


            //context.Auteurs.AddOrUpdate(


            //    a => a.Name,
            //    new Auteur { Name="abdelhafidh",Lastname="AbdelAllah",Adressmail="Test@tes.tn",Profil="Auteur Tahrir"},
            //       new Auteur { Name="Bilel",Lastname="AbdelAllah",Adressmail="Test@tes.tn",Profil="Auteur Tahrir"},
            //          new Auteur { Name="Tahar",Lastname="AbdelAllah",Adressmail="Test@tes.tn",Profil="Auteur Tahrir"},
            //             new Auteur { Name="Mahdi",Lastname="AbdelAllah",Adressmail="Test@tes.tn",Profil="Auteur Tahrir"}
            //    );


            SeedMemberShip();

        }
              private void SeedMemberShip()
        {
            if (!WebSecurity.Initialized)
            {

                WebSecurity.InitializeDatabaseConnection("AuteurContext", "UserProfile", "UserId", "UserName", 
                    autoCreateTables: true);
            }
            


            var roles = (SimpleRoleProvider)Roles.Provider;
            var membership = (SimpleMembershipProvider)Membership.Provider;


            if (!roles.RoleExists("Admin"))
            {
                roles.CreateRole("Admin");


            }

            if (!roles.RoleExists("Author"))
            {
                roles.CreateRole("Author");

            }

            if (membership.GetUser("bilel", false) == null)
            {
                WebSecurity.CreateUserAndAccount("bilel", "m2b2014", new { Mail = "benkh.bilel@gmail.com"}, true);
            }
           if(!roles.GetRolesForUser("bilel").Contains("Admin"))
           {
               roles.AddUsersToRoles(new[] {"bilel"},new [] {"Admin"});
           }
        }


            //  This method will be called after migrating to the latest version.

            //  You can use the DbSet<T>.AddOrUpdate() helper extension method 
            //  to avoid creating duplicate seed data. E.g.
            //
            //    context.People.AddOrUpdate(
            //      p => p.FullName,
            //      new Person { FullName = "Andrew Peters" },
            //      new Person { FullName = "Brice Lambson" },
            //      new Person { FullName = "Rowan Miller" }
            //    );
            //
        }

       
    }

